package com.jeect.productservice.web;

import com.jeect.base.domain.Product;
import com.jeect.base.util.ResponseBody;
import com.jeect.base.util.ResponseCode;
import com.jeect.productservice.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class ProductServerController {
    @Autowired
    private ProductService productService;
    @Autowired
    private HttpServletRequest servletRequest;
    @GetMapping("/product/{sku}")
    public Product getProduct(@PathVariable(required = true) String sku){
        System.out.println("sessionId======"+servletRequest.getSession().getId());
        return productService.getBySku(sku);
      /*  if(product!=null){
            return new ResponseBody(ResponseCode.SUCCESS,product);
        }
        return new ResponseBody(ResponseCode.FAILE);*/
    }
    @GetMapping(name = "/product/")
    public ResponseBody newProduct(@RequestParam("sku")String sku,@RequestParam("skuname")String skuname){
        Product product = new Product();
        product.setSku(sku);
        product.setSkuName(skuname);

        productService.saveProduct(product);

        return new ResponseBody(ResponseCode.SUCCESS);
    }

    @PostMapping(name = "/product/update")
    public ResponseBody updateProduct(@ModelAttribute Product product){
        if(!"".equals(product.getSku())){
            productService.updateProduct(product);
            return new ResponseBody(ResponseCode.SUCCESS);
        }
        return new ResponseBody(ResponseCode.FAILE);
    }
}
